package com.aizuda.boot.modules.flw.controller;

import com.aizuda.boot.modules.flw.entity.dto.DestroyInstanceDTO;
import com.aizuda.boot.modules.flw.entity.dto.FlwProcessInstanceDTO;
import com.aizuda.boot.modules.flw.entity.vo.FlwInstanceVO;
import com.aizuda.boot.modules.flw.flow.FlowHelper;
import com.aizuda.boot.modules.flw.service.IFlwProcessService;
import com.aizuda.bpm.engine.FlowLongEngine;
import com.aizuda.core.api.ApiController;
import com.aizuda.core.api.PageParam;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程实例 前端控制器
 *
 * @author 青苗
 * @since 2023-09-07
 */
@Tag(name = "流程实例")
@RestController
@RequestMapping("/v1/process-instance")
public class ProcessInstanceController extends ApiController {
    @Resource
    private IFlwProcessService flwProcessService;
    @Resource
    private FlowLongEngine flowLongEngine;

    @Operation(summary = "流程实例分页列表")
    @Permission("flw:processInstance:page")
    @PostMapping("/page")
    public Page<FlwInstanceVO> pageInstance(@RequestBody PageParam<FlwProcessInstanceDTO> dto) {
        return flwProcessService.pageInstance(dto.page(), dto.getData());
    }

    @Operation(summary = "获取所有分类流程定义列表")
    @Permission("flw:processInstance:listAll")
    @Parameters({
            @Parameter(name = "processId", description = "流程ID", in = ParameterIn.PATH)
    })
    @PostMapping("/start/{processId}")
    public boolean start(@PathVariable("processId") Long processId) {
        return flowLongEngine.startInstanceById(processId, FlowHelper.getFlowCreator()).isPresent();
    }

    @Operation(summary = "根据流程实例ID获取流程变量")
    @Permission("flw:processInstance:variable")
    @Parameters({
            @Parameter(name = "instanceId", description = "流程实例ID", in = ParameterIn.PATH)
    })
    @PostMapping("/variable/{instanceId}")
    public Map<String, Object> variable(@PathVariable("instanceId") Long instanceId) {
        return flwProcessService.getVariableByInstanceId(instanceId);
    }

    @Operation(summary = "根据流程实例ID删除审流程实例")
    @Permission("flw:processInstance:remove")
    @PostMapping("/remove")
    public boolean remove(@NotEmpty @RequestBody List<Long> instanceIds) {
        return flwProcessService.removeProcessByInstanceIds(instanceIds);
    }

    @Operation(summary = "根据流程实例ID唤醒撤销拒审终止流程实例")
    @Permission("flw:processInstance:resume")
    @Parameters({
            @Parameter(name = "instanceId", description = "流程实例ID", in = ParameterIn.PATH)
    })
    @PostMapping("/resume/{instanceId}")
    public boolean resume(@PathVariable("instanceId") Long instanceId) {
        return flwProcessService.resumeProcessByInstanceId(instanceId);
    }

    @Operation(summary = "根据流程实例ID终止流程实例")
    @Permission("flw:processInstance:terminate")
    @Parameters({
            @Parameter(name = "instanceId", description = "流程实例ID", in = ParameterIn.PATH)
    })
    @PostMapping("/terminate/{instanceId}")
    public boolean terminate(@PathVariable("instanceId") Long instanceId) {
        return flwProcessService.terminateProcessByInstanceId(instanceId);
    }

    @Operation(summary = "作废流程实例")
    @Permission("flw:processInstance:destroy")
    @PostMapping("/destroy")
    public boolean destroy(@Validated @RequestBody DestroyInstanceDTO dto) {
        return flwProcessService.destroyProcessByInstanceId(dto);
    }

}
